<?php
class db
{
	var $connid;
	var $dbname;
	var $querynum = 0;
	var $debug = 1;
	
	function __construct()
	{
		$this->connect(DB_HOST,DB_USER,DB_PWD,DB_NAME,0,'utf8');
	}
	
	function db()
	{
		$this->__construct();
	}
	
	function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $charset = '')
	{
		$func = $pconnect == 1 ? 'mysql_pconnect' : 'mysql_connect';
		if(!$this->connid = @$func($dbhost, $dbuser, $dbpw))
		{
			$this->halt('MySQL server 无法连接！');
			return false;
		}
		if($this->version() > '4.1')
		{
			$serverset = $charset ? "character_set_connection='$charset',character_set_results='$charset',character_set_client=binary" : '';
			$serverset .= $this->version() > '5.0.1' ? ((empty($serverset) ? '' : ',')." sql_mode='' ") : '';
			$serverset && mysql_query("SET $serverset", $this->connid);
		}
		if($dbname && !@mysql_select_db($dbname , $this->connid))
		{
			$this->halt("数据库$dbname无法连接 ");
			return false;
		}
		$this->dbname = $dbname;
		return $this->connid;
	}

	function query($sql)
	{
		if(!$query = @mysql_query($sql , $this->connid))
		{
			$this->halt('执行SQL语句失败', $sql);
			return false;
		}
		$this->querynum++;
		return $query;
	}


	function getone($sql)
	{
		$query = $this->query($sql);
		$rs = $this->fetch_array($query);
		$this->free_result($query);
		return $rs ;
	}

	function getlist($sql)
	{
		$array = array();
		$result = $this->query($sql);
		while($r = $this->fetch_array($result))
		{
			$array[] = $r;
		}
		$this->free_result($result);
		return $array;
	}

	function insert($tablename, $array)
	{
		$this->check_fields($tablename, $array);
		return $this->query("INSERT INTO `$tablename`(`".implode('`,`', array_keys($array))."`) VALUES('".implode("','", $array)."')");
	}

	function update($tablename, $array, $where = '')
	{
		$this->check_fields($tablename, $array);
		if($where)
		{
			$sql = '';
			foreach($array as $k=>$v)
			{
				$sql .= ", `$k`='$v'";
			}
			$sql = substr($sql, 1);
			$sql = "UPDATE `$tablename` SET $sql WHERE $where";
		}
		else
		{
			$sql = "REPLACE INTO `$tablename`(`".implode('`,`', array_keys($array))."`) VALUES('".implode("','", $array)."')";
		}
		return $this->query($sql);
	}

	
	function check_fields($tablename, $array)
	{
		$fields = $this->get_fields($tablename);
		foreach($array AS $k=>$v)
		{
			if(!in_array($k,$fields))
			{
				$this->halt('MySQL Query Error', "Unknown column '$k' in field list");
				return false;
			}
		}
	}

	function get_fields($table)
	{
		$fields = array();
		$result = $this->query("SHOW COLUMNS FROM $table");
		while($r = $this->fetch_array($result))
		{
			$fields[] = $r['Field'];
		}
		$this->free_result($result);
		return $fields;
	}
	
	function fetch_array($query)
	{
		return mysql_fetch_array($query);
	}

	function affected_rows()
	{
		return mysql_affected_rows($this->connid);
	}

	function num_rows($query)
	{
		return mysql_num_rows($query);
	}

	function free_result(&$query)
	{
		return mysql_free_result($query);
	}

	function insert_id()
	{
		return mysql_insert_id($this->connid);
	}

	function field_exists($table, $field)
	{
		$fields = $this->get_fields($table);
		return in_array($field, $fields);
	}

	function version()
	{
		return mysql_get_server_info($this->connid);
	}

	function close()
	{
		return mysql_close($this->connid);
	}

	function halt($message = '', $sql = '')
	{
		$this->errormsg =  "<b>MySQL Query : </b>$sql <br />
							<b> MySQL Error : </b>".@mysql_error($this->connid)." <br /> 
							<b>MySQL Errno : </b>".intval(@mysql_errno($this->connid))." <br />
							<b> Message : </b> $message";
		if($this->debug)
		{
			$msg = (defined('IN_ADMIN') || DEBUG) ? $this->errormsg : "Bad Request. $LANG[illegal_request_return]";
			echo $msg;
			exit;
		}
	}
}
?>